package dk.rafaelcouto.PhoneTone_Extractor;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Inet4Address;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class AudioApp extends Activity {

	static final int ERROR_MSG = -1;
	static final int MY_MSG = 1;
	static final int SILENCE = 3;
	static final String TAG = "AudioApp";
	static final int TIME_MSG = 2;
	static int run_average = 50;
	public Bitmap bitmap;
	public Bitmap bmp;
	private Button btn_fft_button;
	private SeekBar btn_seekBar1;
	private Button btn_start_button;
	private Button btn_stop_button;
	private int chars = 10;
	private Button clear_button;
	private String cpuinfo = "";
	ImageView imageview;

	public Handler mhandle = new Handler() {
		private String translate_tone(String paramAnonymousString) {
			if (paramAnonymousString.equals("d"))
				return " dialtone ";
			return paramAnonymousString;
		}

		public void handleMessage(Message paramAnonymousMessage) {
			switch (paramAnonymousMessage.what) {
			case 0:
			default:
				super.handleMessage(paramAnonymousMessage);
			case 1:

				AudioApp.this.tv_tv1.setGravity(5);
				final AudioApp this$0 = AudioApp.this;
				AudioApp.this.numeros += translate_tone(paramAnonymousMessage.obj
						.toString());
				AudioApp.this.numeros2 = (translate_tone(paramAnonymousMessage.obj
						.toString()) + AudioApp.this.numeros2);
				AudioApp.this.tv_tv1
						.setText((CharSequence) AudioApp.this.numeros);
				AudioApp.this.textView2
						.setText((CharSequence) AudioApp.this.numeros2);
				AudioApp.this.textView1.setText((CharSequence) "");
				if (AudioApp.this.numeros.length() > AudioApp.this.chars) {
					AudioApp.this.numeros = AudioApp.this.numeros.subSequence(
							1, AudioApp.this.chars).toString();
					return;
				}
				break;
			case 2:
				AudioApp.this.st1 = Integer.parseInt(paramAnonymousMessage.obj
						.toString());
				AudioApp.this.tv1.setText("Processing time: "
						+ AudioApp.this.st1 + " ms");
				return;
			case -1:
				Toast.makeText(AudioApp.this.mContext,
						"Error " + paramAnonymousMessage.obj, Toast.LENGTH_LONG).show();
				AudioApp.this.stop_meter();
				return;
			case 3:
			}
			AudioApp.this.textView1.setText("(Silence detected)");
		}
	};
	public Bitmap mybitmap;
	public Bitmap newbmp;
	private String numeros = "";
	private String numeros2 = "";
	Paint paint;
	private String performance = "";
	private int sensitivity = 50;
	private int st = 50;
	private int st1 = 50;
	private int started = 0;
	private TextView textView1;
	private TextView textView2;
	private TextView tv1;
	private TextView tv_info;
	private TextView tv_test;
	private TextView tv_tv1;
	PhoneToneProcessor mEngine = null;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
//		Eula.show(this);
		this.btn_start_button = ((Button) findViewById(R.id.start_button));
		this.btn_stop_button = ((Button) findViewById(R.id.stop_button));
		this.btn_fft_button = ((Button) findViewById(R.id.fft_button));
		this.clear_button = ((Button) findViewById(R.id.clear_button));
		this.tv_tv1 = ((TextView) findViewById(R.id.tv_tv1));
		this.tv1 = ((TextView) findViewById(R.id.tv1));
		this.mEngine = new PhoneToneProcessor(this.mhandle, this.mContext);
		this.tv_info = ((TextView) findViewById(R.id.tv_info));
		this.textView1 = ((TextView) findViewById(R.id.textView1));
		this.textView2 = ((TextView) findViewById(R.id.textView2));
		this.tv_test = ((TextView) findViewById(R.id.tv_test));
		this.btn_seekBar1 = ((SeekBar) findViewById(R.id.seekBar1));
		int i = getWindowManager().getDefaultDisplay().getWidth();
		this.btn_seekBar1.setProgress(50);
		this.btn_stop_button.setEnabled(false);
		this.btn_fft_button.setVisibility(View.GONE);
		this.tv_test.setText("Sensitivity bar");
		this.textView2
				.setText("Try to adjust the sensitivity bar to find the best point between noise and signal, it's take some time in the beginning but after this you will find the right position easily.    Please send comments to ToneExtractor@gmail.com");
		this.chars = (-1 + i / 16);
		this.btn_start_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramAnonymousView) {
				AudioApp.this.btn_start_button.setEnabled(false);
				AudioApp.this.btn_stop_button.setEnabled(true);
				AudioApp.this.tv1.setText("");
				if (AudioApp.this.started == 0) {
					AudioApp.this.tv_tv1.setText("");
					AudioApp.this.textView2.setText("");
					AudioApp.this.started = 1;
				}
				AudioApp.this.mEngine.start_engine();
			}
		});
		this.btn_fft_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramAnonymousView) {
				AudioApp.this.tv_info.setText("Performance index (0-50) : "
						+ AudioApp.this.set_up());
			}
		});
		this.btn_seekBar1.setProgress(this.sensitivity);
		this.btn_seekBar1
				.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
					public void onProgressChanged(
							SeekBar paramAnonymousSeekBar,
							int paramAnonymousInt, boolean paramAnonymousBoolean) {
						AudioApp.this.sensitivity = (5 * AudioApp.this.btn_seekBar1
								.getProgress());
						AudioApp.this.mEngine
								.setSensitivity(AudioApp.this.sensitivity);
					}

					public void onStartTrackingTouch(
							SeekBar paramAnonymousSeekBar) {
					}

					public void onStopTrackingTouch(
							SeekBar paramAnonymousSeekBar) {
					}
				});
		this.btn_stop_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramAnonymousView) {
				AudioApp.this.btn_start_button.setEnabled(true);
				AudioApp.this.btn_stop_button.setEnabled(false);
				AudioApp.this.mEngine.stop_engine();
				AudioApp.this.mEngine.stopLogging();
			}
		});
		this.clear_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramAnonymousView) {
				AudioApp.this.tv_tv1.setText("");
				AudioApp.this.textView1.setText("");
				AudioApp.this.textView2.setText("");
				AudioApp.this.numeros2 = "";
				AudioApp.this.numeros = "";
			}
		});
		this.performance = set_up();
		ReadCPUinfo();
		Toast.makeText(this, "Performance index (0-100) : " + this.performance,
				Toast.LENGTH_LONG).show();
		this.tv_tv1.setText("Ready!");
	}

	public void start(View view) {
		AudioApp.this.mEngine.start_engine();
	}

	private String ReadCPUinfo() {
		Object localObject = "";
		try {
			InputStream localInputStream = new ProcessBuilder(new String[] {
					"/system/bin/cat", "/proc/cpuinfo" }).start()
					.getInputStream();
			byte[] arrayOfByte = new byte[1024];
			while (true) {
				if (localInputStream.read(arrayOfByte) == -1) {
					localInputStream.close();
					String str1 = "Performance index:\t" + set_up() + "\t\r\n"
							+ (String) localObject;
					this.cpuinfo = str1;
					transmit(str1.replaceAll("\\r", " ").replaceAll("\\n", " "));
					return (String) localObject;
				}
				String str2 = new String(arrayOfByte);
				String str3 = localObject + str2.trim();
				localObject = str3;
			}
		} catch (IOException localIOException) {
			while (true)
				localIOException.printStackTrace();
		}
	}

	private String md5(String paramString) {
		try {
			MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
			localMessageDigest.reset();
			localMessageDigest.update(paramString.getBytes());
			byte[] arrayOfByte = localMessageDigest.digest();
			int i = arrayOfByte.length;
			StringBuilder localStringBuilder = new StringBuilder(i << 1);
			for (int j = 0;; j++) {
				if (j >= i)
					return localStringBuilder.toString();
				localStringBuilder.append(Character.forDigit(
						(0xF0 & arrayOfByte[j]) >> 4, 16));
				localStringBuilder.append(Character.forDigit(
						0xF & arrayOfByte[j], 16));
			}
		} catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
			localNoSuchAlgorithmException.printStackTrace();
		}
		return null;
	}

	private boolean transmit(String paramString) {
		try {
			OutputStream localOutputStream = new Socket(
					Inet4Address.getByName("Rafaelcouto.dk"), 80)
					.getOutputStream();
			String str = "data1=" + paramString + "&process=CADASTRAR";
			byte[] arrayOfByte = str.getBytes();
			localOutputStream
					.write(("POST /cadastro/android/cadastra.php HTTP/1.1\r\nAccept: text/html, application/xhtml+xml, */*\r\nReferer: http://www.rafaelcouto.dk/cadastro/android/index.php\r\nAccept-Language: da-DK\r\nUser-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Encoding: gzip, deflate\r\nHost: www.rafaelcouto.dk\r\n"
							+ "Content-Length: "
							+ arrayOfByte.length
							+ "\r\n\r\n" + str).getBytes());
			return true;
		} catch (UnknownHostException localUnknownHostException) {
			while (true)
				Log.e("AudioApp", "Server Not Found");
		} catch (IOException localIOException) {
			while (true)
				Log.e("AudioApp", "Couldn't open socket");
		}
	}

	protected void onDraw(Canvas paramCanvas) {
		this.imageview.draw(paramCanvas);
		paramCanvas.drawColor(-16776961);
		this.mybitmap = BitmapFactory
				.decodeResource(getResources(), 2130837506);
		paramCanvas.drawBitmap(this.mybitmap, 0.0F, 0.0F, this.paint);
	}

	public boolean onOptionsItemSelected(MenuItem paramMenuItem) {
		switch (paramMenuItem.getItemId()) {
		default:
			return true;
		case 2131165203:
			Toast.makeText(
					this,
					"Try to adjust the sensitivity bar to find the best point between noise and signal, it's take some time in the beginning but after this you will find the right position easily.    Please send comments to ToneExtractor@gmail.com",
					Toast.LENGTH_LONG).show();
			return true;
		case 2131165204:
			Toast.makeText(this, this.cpuinfo, Toast.LENGTH_LONG).show();
			return true;
		case 2131165205:
		}
		this.mEngine.stop_engine();
		super.onStop();
		super.onDestroy();
		finish();
		return true;
	}

	static/* synthetic */void access$2(final AudioApp audioApp,
			final String numeros) {
		audioApp.numeros = numeros;
	}

	public void onPause() {
		this.mEngine.stop_engine();
		super.onPause();
	}

	public void onResume() {
		super.onResume();
	}

	public void onStop() {
		this.mEngine.stop_engine();
		super.onStop();
	}

	public String set_up() {
		long currentTimeMillis = 0L;
		for (int i = 0; i < 3; ++i) {
			if (i == 2) {
				currentTimeMillis = System.currentTimeMillis();
			}
			final Complex[] array = new Complex[512];
			final Complex complex = new Complex(0);
			for (int j = 0; j < 512; ++j) {
				if (j % 2 == 0) {
					array[j] = new Complex(1.0, 0.0);
				} else {
					array[j] = new Complex(-1.0, 0.0);
				}
			}
			final Complex[] array2 = new Complex[512];
			final Complex[] ifft = FFT.ifft(FFT.fft(array));
			for (int k = 0; k < 512; ++k) {
				complex.plus(ifft[k]);
			}
		}
		return new StringBuilder()
				.append((int) (10.0 * Math.log(20000.0 / (System
						.currentTimeMillis() - currentTimeMillis) - 23.0)))
				.toString();
	}

	public void start_meter() {
		this.mEngine.start_engine();
	}

	public void stop_meter() {
		this.mEngine.stop_engine();
	}
}
